package com.endbd.systemmanagement.service.permission.impl;

import com.endbd.feignapi.model.system.Job;
import com.endbd.feignapi.model.system.Module;
import com.endbd.feignapi.model.system.User;
import com.endbd.systemmanagement.dao.permission.IPermissionDao;
import com.endbd.systemmanagement.service.permission.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class PermissionService implements IPermissionService {

    @Autowired
    private IPermissionDao permissionDao;

    @Override
    public User loginCheck(String username, String password) {
        return permissionDao.queryUserByUsernameAndPassword(username, password);
    }

    @Override
    public Job queryJobByUser(User user) {
        return permissionDao.queryJobByUser(user);
    }

    @Override
    public List<List<Module>> queryModuleByUser(User user) {
        List<Module> modules = permissionDao.queryModuleByUser(user);
        List<List<Module>> moduleLists = new ArrayList<>();
        for (Module module : modules) {
            if (module.getModulePid() == 0 && "1".equals(module.getModuleState())) {
                List<Module> moduleList = new ArrayList<>();
                moduleList.add(module);
                moduleLists.add(moduleList);
            }
        }

        for (List<Module> moduleList : moduleLists) {
            Integer moduleId = moduleList.get(0).getModuleId();
            for (Module module : modules) {
                if (Objects.equals(module.getModulePid(), moduleId) && "1".equals(module.getModuleState())) {
                    moduleList.add(module);
                }
            }
        }

        return moduleLists;
    }
}
